﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using DBLayer.Datacontext;
using ModelLayer;

namespace DBLayer
{
    public class DbCompanyOp
    {
        public DbCompanyOp()
        { }


        //Converterer ModelLayer.Company til DataClass DbCompany
        public DbCompany ConvertCompanyToDbCompany(Company c)
        {
            DbCompany dbCompany = new DbCompany();



            //Always not null
            dbCompany.CompanyId = c.CompanyId;
            dbCompany.CompanyCode = c.CompanyCode;
            dbCompany.DatePuplished = c.DatePublished;

            //Kan indholde nulls
            if (c.ZipCode != null)
            {
                dbCompany.ZipCode = (int)c.ZipCode.ZipCode;
            }
            if (c.CompanyName != null)
            {
                dbCompany.CompanyName = c.CompanyName;
            }
            if (c.Address != null)
            {
                dbCompany.Address = c.Address;
            }
            if (c.CompanyLogo != null)
            {
                dbCompany.CompanyLogo = c.CompanyLogo;
            }
            if (c.Email != null)
            {
                dbCompany.Email = c.Email;
            }
            if (c.Fax != null)
            {
                dbCompany.Fax = c.Fax;
            }
            if (c.Mobile != null)
            {
                dbCompany.Mobile = c.Mobile;
            }
            if (c.Phone != null)
            {
                dbCompany.Phone = c.Phone;
            }
            if (c.WebPage != null)
            {
                dbCompany.WebPage = c.WebPage;
            }


            return dbCompany;

        }



        // Converts dbmodel to modellayer
        public Company ConvertDbCompanytoCompany(DbCompany dbCompany)
        {
            Company company = new Company();

            //Always not null
            company.CompanyId = dbCompany.CompanyId;
            company.CompanyCode = dbCompany.CompanyCode;
            //Possible nulls
            if (dbCompany.Address != null)
            {
                company.Address = dbCompany.Address;
            }
            if (dbCompany.CompanyLogo != null)
            {
                company.CompanyLogo = dbCompany.CompanyLogo;
            }
            if (dbCompany.CompanyName != null)
            {
                company.CompanyName = dbCompany.CompanyName;
            }
            if (dbCompany.DatePuplished != null)
            {
                company.DatePublished = (DateTime)dbCompany.DatePuplished;
            }
            if (dbCompany.Email != null)
            {
                company.Email = dbCompany.Email;
            }
            if (dbCompany.Fax != null)
            {
                company.Fax = dbCompany.Fax;
            }
            if (dbCompany.Mobile != null)
            {
                company.Mobile = dbCompany.Mobile;
            }
            if (dbCompany.Phone != null)
            {
                company.Phone = dbCompany.Phone;
            }
            if (dbCompany.WebPage != null)
            {
                company.WebPage = dbCompany.WebPage;
            }
            if (dbCompany.ZipCode != null)
            {
                company.ZipCode.ZipCode = (int)dbCompany.ZipCode;
            }


            return company;
        }




        private List<Company> getCompanies(int[] compId)
        {
            DataClassesDataContext db = new DataClassesDataContext();

            List<DbCompany> dbComp = db.DbCompanies.Where(c => compId.Contains(c.CompanyCode)).ToList();
            List<Company> companies = dbComp.Select(dbc => ConvertDbCompanytoCompany(dbc)).ToList();
            return companies;
        }

        public Company GetCompanyByCompanyName(string name)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var dbCompany = db.DbCompanies.SingleOrDefault(co => co.CompanyName == name);
            if (dbCompany != null)
            {
                return ConvertDbCompanytoCompany(dbCompany);
            }
            return null;
        }


    }
}
